home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 110 / EnigmaAmiga110CD.iso / dalla rivista / host contacted / ksc_grepmem.lha / hex.asm < prev   
Assembly Source File  |  2000-02-21  |  1KB  |  61 lines

  1. ; hex.asm - convert ascii hex digits to a real hex number
  2. ; by Kyzer/CSG
  3. ; $VER: hex.asm 1.1 (21.02.00)
  4.  
  5. ; IN:
  6. ;  A0=string containing ASCII hex digits
  7.  
  8. ; OUT:
  9. ;  D0=value of hex string (32 bit)
  10. ;  D1=validity: -1 = valid number, 0 = invalid number
  11.  
  12. ; ALSO TRASHED
  13. ;  A0/A1/D2/D3
  14.  
  15.     cmp.b    #'$',(a0)+        ; skip leading '$'
  16.     beq.s    .start
  17.     subq.l    #1,a0
  18.  
  19.     cmp.b    #'0',(a0)        ; skip leading '0x'
  20.     bne.s    .start
  21.     cmp.b    #'x',1(a0)
  22.     bne.s    .start
  23.     addq.l    #2,a0
  24.  
  25. .start    moveq    #0,d0
  26.     moveq    #8-1,d1
  27. .nxtlet    moveq    #0,d2
  28.     move.b    (a0),d2
  29.  
  30.     ; d2 = asciichar = c; convert c -> x, where
  31.     ; c = "0":"9" = 48:57    -> x = 0:9
  32.     ; c = "A":"F" = 65:69    -> x = 10:15
  33.     ; c = "a":"f" = 97:102    -> x = 10:15
  34.  
  35.     sub.b    #48,d2        ; x = c-"0"        [if c="0":"9" then x=0:9  ]
  36.     bmi.s    .nothex        ; if x < 0 then goto FAIL
  37.     cmp.b    #9,d2
  38.     bls.s    .hexok        ; if x <= 9 then goto OK
  39.     subq.b    #7,d2        ; x-=("A"-"0")+10  [if c="A":"F" then x=10:15]
  40.     cmp.b    #15,d2
  41.     bls.s    1$        ; if x > 15 then
  42.     sub.b    #32,d2        ; x -= "a"-"A"     [if c="a":"f" then x=10:15]
  43. 1$    cmp.b    #15,d2        ; if x > 15 then goto FAIL
  44.     bhi.s    .nothex
  45.     tst.b    d2
  46.     bmi.s    .nothex        ; if x >= 0 then goto OK
  47.  
  48. .hexok    asl.l    #4,d0
  49.     add.b    d2,d0
  50.  
  51.     addq.l    #1,a0
  52.     tst.b    (a0)
  53.     beq.s    .done
  54.  
  55.     dbra    d1,.nxtlet
  56.  
  57. .done    moveq    #-1,d1    ; OK
  58.     rts
  59. .nothex    moveq    #0,d1    ; FAIL
  60.     rts
  61.